package cn.edu.ujs.coor.controller;

import cn.edu.ujs.coor.common.annotation.DataLevel;
import cn.edu.ujs.coor.model.Group;
import cn.edu.ujs.coor.model.User;
import cn.edu.ujs.coor.request.IdRequest;
import cn.edu.ujs.coor.request.group.GroupAddRequest;
import cn.edu.ujs.coor.request.group.GroupConfigRequest;
import cn.edu.ujs.coor.request.group.GroupPageRequest;
import cn.edu.ujs.coor.request.group.GroupUpdateRequest;
import cn.edu.ujs.coor.response.JsonResponse;
import cn.edu.ujs.coor.response.PageResponse;
import cn.edu.ujs.coor.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping(value = "/group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @DataLevel
    @RequestMapping(value = "/list")
    public JsonResponse list (@RequestBody GroupPageRequest request, User user) {
        PageResponse response = groupService.page(request);
        return JsonResponse.success(response);
    }

    @RequestMapping(value = "/add")
    public JsonResponse add (@RequestBody @Valid GroupAddRequest request, User user) {
        request.setUserId(user.getId());
        request.setOrganizationId(user.getOrganizationId());
        groupService.create(request);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/config")
    public JsonResponse config (@RequestBody @Valid GroupConfigRequest request) {
        groupService.config(request);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/info")
    public JsonResponse config (@RequestBody @Valid IdRequest request) {
        return JsonResponse.success(groupService.info(request));
    }

    @RequestMapping(value = "/update")
    public JsonResponse update (@RequestBody @Valid GroupUpdateRequest request) {
        groupService.update(request);
        return JsonResponse.success();
    }

    @RequestMapping(value = "/delete")
    public JsonResponse delete (@RequestBody @Valid IdRequest request) {
        groupService.delete(request);
        return JsonResponse.success();
    }
}
